package itsm.isperp.module.service.announcement; 
 
import java.util.List;

import itsm.isperp.framework.data.dialect.DialectFactory;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.announcement.AnnoAnnouncement;
import itsm.isperp.module.entity.knowledge.KngKnowledgeItem;
import itsm.isperp.module.repository.announcement.AnnouncementMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jdbc.support.DatabaseType;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2016-08-26 22:15
 */
@Service
public class AnnouncementService extends BaseService<AnnoAnnouncement> {

	@Autowired
	protected AnnouncementMapper announcementMapper;

	@Override
	public AnnouncementMapper getRepository() {
		return this.announcementMapper;
	}
	
	public List<AnnoAnnouncement> getNewTop10Item(int num) {

		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.setRows(num);
		req.setSort("create_date", Direction.DESC);

		String contentsSql = null;
		if (DialectFactory.getDatabaseType() == DatabaseType.MYSQL) {
			contentsSql = "LEFT(title," + 26 + ") AS title";
		} else {
			contentsSql = "to_char(SUBSTR(title,0," + 26 + ")) AS title";
		}
		
		req.addFilterRule("status", JqGridFilterRuleOp.bw, "已发布");
		req.setSearchString(" (NOW()<effective_date OR effective_date IS NULL) ");

		req.setSelectColumns("id", contentsSql,"create_date","effective_date","importance_level","status","contents");
		return announcementMapper.findAllByRequest(req);
	}
	
}
